package com.mall.module.system.controller;

import com.github.pagehelper.PageInfo;
import com.mall.common.result.Result;
import com.mall.module.system.domain.dto.RoleAuthDTO;
import com.mall.module.system.domain.entity.SysRole;
import com.mall.module.system.domain.entity.SysUser;
import com.mall.module.system.domain.dto.RoleCreateDTO;
import com.mall.module.system.domain.dto.RoleUpdateDTO;
import com.mall.module.system.domain.vo.PermissionTreeVO;
import com.mall.module.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "系统用户角色接口")
@RestController
@RequestMapping("/api/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    //1. 分页查询角色列表
    @ApiOperation(value = "分页查询角色列表")
//    @PreAuthorize("hasAuthority('sys:role:view')")
    @GetMapping
    public Result<PageInfo<SysRole>> listRoles(
        @RequestParam(defaultValue = "1") int pageNum,
        @RequestParam(defaultValue = "10") int pageSize,
        @RequestParam(required = false) String roleName,
        @RequestParam(required = false) Integer status) {
        return Result.success(sysRoleService.listRoles(pageNum, pageSize, roleName, status));
    }

    //2. 获取角色详情
    @ApiOperation(value = "获取角色详情")
//    @PreAuthorize("hasAuthority('sys:role:view')")
    @GetMapping("/{roleId}")
    public Result<SysRole> getRoleDetail(@PathVariable Long roleId) {
        SysRole role = sysRoleService.getRoleById(roleId);
        return Result.success(role);
    }



    //3. 新增角色
    @ApiOperation(value = "创建角色")
//    @PreAuthorize("hasAuthority('sys:role:create')")
    @PostMapping
    public Result<?> createRole(@Valid @RequestBody RoleCreateDTO dto) {
        boolean success = sysRoleService.createRole(dto);
        return success ? Result.success() : Result.error(401,"创建角色失败");
    }


    //4. 修改角色
//    @PreAuthorize("hasAuthority('sys:role:update')")
    @ApiOperation(value = "修改角色")
    @PutMapping("/{roleId}")
    public Result<Boolean> updateRole(
        @PathVariable Long roleId,
        @Valid @RequestBody RoleUpdateDTO dto) {
        return Result.success(sysRoleService.updateRole(roleId, dto));
    }

    //5. 删除角色
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/{roleId}")
    public Result<?> deleteRole(@PathVariable Long roleId) {
        boolean success = sysRoleService.deleteRole(roleId);
        return success ? Result.success() : Result.error(401,"删除角色失败");
    }

@ApiOperation(value = "分配权限")
    @PostMapping("/{roleId}/authorizations")
    public Result<Boolean> assignRoleAuthorizations(
        @PathVariable Long roleId,
        @RequestBody RoleAuthDTO dto) {
        return Result.success(sysRoleService.assignRoleAuthorizations(roleId, dto));
    }

    @ApiOperation(value = "获取角色菜单")
    @GetMapping("/{roleId}/menus")
    public Result<List<Long>> getRoleMenuIds(@PathVariable Long roleId) {
        List<Long> menuIds = sysRoleService.getRoleMenuIds(roleId);
        return Result.success(menuIds);
    }
    @ApiOperation(value = "获取角色权限")
    @GetMapping("/{roleId}/permissions")
    public Result<List<Long>> getRolePermissionIds(@PathVariable Long roleId) {
        List<Long> permissionIds = sysRoleService.getRolePermissionIds(roleId);
        return Result.success(permissionIds);
    }
    @ApiOperation(value = "获取角色用户")
    @GetMapping("/{roleId}/users")
    public Result<List<SysUser>> getRoleUsers(@PathVariable Long roleId) {
        List<SysUser> users = sysRoleService.getRoleUsers(roleId);
        return Result.success(users);
    }
    @ApiOperation(value = "分配用户")
    @PostMapping("/{roleId}/users")
    public Result<Void> assignUsersToRole(
        @PathVariable Long roleId,
        @RequestBody List<Long> userIds) {
        boolean success = sysRoleService.assignUsersToRole(roleId, userIds);
        return success ? Result.success() : Result.error(401,"分配用户失败");
    }
    @ApiOperation(value = "获取角色权限树")
    @GetMapping("/{roleId}/permission-tree")
    public Result<PermissionTreeVO> getRolePermissionTree(@PathVariable Long roleId) {
        PermissionTreeVO tree = sysRoleService.getRolePermissionTree(roleId);
        return Result.success(tree);
    }
    @ApiOperation(value = "获取权限树")
    @GetMapping("/permission-tree")
    public Result<PermissionTreeVO> getFullPermissionTree() {
        PermissionTreeVO tree = sysRoleService.getFullPermissionTree();
        return Result.success(tree);
    }

    //6. 分配权限（为角色分配菜单和权限）
//    @PostMapping("/{roleId}/menus")
//    public Result<Void> assignRoleMenus(
//        @PathVariable Long roleId,
//        @RequestBody List<Long> menuIds) {
//        boolean success = sysRoleService.assignRoleMenus(roleId, menuIds);
//        return success ? Result.success() : Result.error(401,"分配菜单失败");
//    }
//
//    @PostMapping("/{roleId}/permissions")
//    public Result<Void> assignRolePermissions(
//        @PathVariable Long roleId,
//        @RequestBody List<Long> permissionIds) {
//        boolean success = sysRoleService.assignRolePermissions(roleId, permissionIds);
//        return success ? Result.success() : Result.error(401,"分配权限失败");
//    }
}
